package com.youlu.campus.admin.wechat;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.AppUrlLink;
import com.youlu.campus.entity.VO.req.AppUrlLinkQueryVO;
import com.youlu.campus.service.platform.AppUrlLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class WechatAppUrlLinkController {
    @Autowired
    private AppUrlLinkService appUrlLinkService;

    @PostMapping("/wechat/app-link/list")
    public RestResult list(@RequestBody @Valid AppUrlLinkQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<AppUrlLink> datas = appUrlLinkService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/wechat/app-link/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = appUrlLinkService.delete(id);
        if (re) {
            return RestResultBuilder.builder().success().data(re).build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
